﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using DataLayer.DummaDTO;
using Common.DTO;

namespace DataLayer
{
    /// <summary>
    /// Saves Data to Database
    /// </summary>
    [Obsolete("Använd RepositoryBase istället! /Oskar")]
    public class SaveDBInfo
    {                
        ///// <summary>
        ///// Registrerar en ny spelare,
        ///// </summary>
        ///// <param name="name" password="Lösenord" emil="Mail"></param>
        ///// <returns></returns>
        //public static Boolean SavePlayerNameNameInfoDB(string name, string password, string email)
        //{
        //    var entities = new GameDbEntities();

        //    var nySpelare = new PlayerEntity {Name = name, Mail = email, Password = password};
            
        //    entities.AddToPlayer(nySpelare);
        //    int saveChanges = entities.SaveChanges();

        //    if(saveChanges != 0)
        //        return true;

        //    return false;
        //}

        ///// <summary>
        ///// Deletes a PlayerGame by id, if no Player in PlayerGame is left, the Game is deleted too.
        ///// </summary>
        ///// <param name="playerId"></param>
        ///// <param name="sessionId"></param>
        ///// <returns></returns>
        //public static bool DeletePlayerGame(int playerId, int sessionId)
        //{
        //    try
        //    {

        //        var entities = new GameDbEntities();

        //        foreach (PlayerGameEntity playerSession in entities.PlayerGame)
        //        {
        //            if (playerSession.GameID == sessionId && playerSession.PlayerID == playerId)
        //            {
        //                entities.DeleteObject(playerSession);
        //            }
        //        }

        //        var specificSession = entities.Game.Where(s => s.ID == sessionId).SingleOrDefault();
        //        if (specificSession.PlayerGame.Count.Equals(0))
        //        {
        //            entities.DeleteObject(specificSession);
        //        }

        //        entities.SaveChanges();
        //        return true;
                
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}


        //public static bool DeleteGame(int gameId)
        //{
        //    try
        //    {                
        //        var entities = new GameDbEntities();
                
        //        //Get the specific session, loop and delete all playersessions because of foreign key constraints
        //        var session = entities.Game.Where(s => s.ID == gameId).SingleOrDefault();                
        //        foreach(PlayerGameEntity playerSession in entities.PlayerGame)
        //        {
        //            if (playerSession.GameID == gameId)
        //            {                       
        //                entities.PlayerGame.DeleteObject(playerSession);
        //            }
        //        }
        //        entities.DeleteObject(session);

        //        entities.SaveChanges();
        //        return true;
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}

        /// <summary>
        /// Create a Game, supposed to return the id of the session
        /// </summary>
        /// <param name="gameTypeId"></param>
        /// <param name="rounds"></param>
        /// <returns>id of the session</returns>
        public static int CreateGame(int gameTypeId, int rounds)
        {
            try
            {
                var entities = new GameDbEntities();

                var game = new GameEntity
                {
                    GameTypeID = gameTypeId,
                    NoOfGameRounds = rounds,
                    DateStart = DateTime.Now,
                    ActiveGame = false,
                };
                entities.AddToGame(game);
                entities.SaveChanges();

                return game.ID;
            }
            catch
            {
                return -1;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gameId"></param>
        /// <param name="playerId"></param>
        /// <returns></returns>
        public static int CreatePlayerGame(int gameId, int playerId)
        {
            try
            {
                var entities = new GameDbEntities();

                var playerGame = new PlayerGameEntity
                {
                    GameID = gameId,
                    PlayerID = playerId,
                };

                entities.AddToPlayerGame(playerGame);
                entities.SaveChanges();

                return playerGame.ID;
            }
            catch
            {
                return -1;
            }
        }

        public static int CreateGameRound(int gameId)
        {
            var entities = new GameDbEntities();
            var gameRound = new GameRoundEntity 
            { 
                GameID = gameId, 
                Date = DateTime.Now 
            };
            entities.AddToGameRound(gameRound);
            entities.SaveChanges();

            return gameRound.ID;
        }

        //public static bool UpdateGameRounds(int gameId, int rounds)
        //{
        //    try
        //    {
        //        var entites = new GameDbEntities();
        //        GameEntity game = entites.Game.Where(s => s.ID == gameId).SingleOrDefault();

        //        game.NoOfGameRounds = rounds;

        //        entites.SaveChanges();

        //        return true;
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}

        ///// <summary>
        ///// Adds a new player to an existing game
        ///// </summary>
        ///// <param name="playerId"></param>
        ///// <param name="gameId"></param>
        ///// <returns>bool</returns>
        //public static bool AddPlayerToGame(int playerId, int gameId)
        //{
        //    try
        //    {
        //        var entities = new GameDbEntities();

        //        var playerGame = new PlayerGameEntity
        //        {
        //            PlayerID = playerId,
        //            GameID = gameId,
        //        };
        //        entities.AddToPlayerGame(playerGame);
        //        entities.SaveChanges();
        //        return true;
        //    }
        //    catch
        //    {
        //        return false;
        //    }

        //}

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="gameId"></param>
        ///// <param name="playerId"></param>
        ///// <param name="playerChoiceId"></param>
        ///// <returns></returns>
        //public static bool MakePlayerChoice(int gameId, int playerId, int playerChoiceId)
        //{
        //    try
        //    {
        //        var entities = new GameDbEntities();
        //        //Gets Round for current GameId
        //        var gameRounds = entities.GameRound.Where(x => x.GameID == gameId).ToList();
        //        //Gets the round with newest date
        //        var latestRound = gameRounds.OrderByDescending(x => x.Date).FirstOrDefault();

        //        var playerChoice = new PlayerChoiceEntity
        //        {
        //            PlayerID = playerId,
        //            Choice = playerChoiceId,
        //            GameRoundID = latestRound.ID
        //        };
        //        entities.AddToPlayerChoice(playerChoice);
        //        entities.SaveChanges();

        //        return true;
        //    }
        //    catch
        //    {
        //        //throw new NotImplementedException();
        //        return false;
        //    }
        //}
        
        //public static bool SetWinner(int gameId, int playerId)
        //{
        //    var entities = new GameDbEntities();

        //    var game = entities.Game.FirstOrDefault(g => g.ID == gameId);

        //    if (game.Winner == null)
        //    {
        //        game.ActiveGame = false;
        //        game.Winner = playerId;

        //        entities.SaveChanges();

        //        return true;
        //    }

        //    return false;
        //}

        //public static bool JoinGame(int gameId)
        //{
        //    var entities = new GameDbEntities();

        //    var game = entities.Game.FirstOrDefault(g => g.ID == gameId);

        //    if (game.ActiveGame == false)
        //    {
        //        game.ActiveGame = true;

        //        entities.SaveChanges();

        //        return true;
        //    }

        //    return false;
        //}

        //public static bool JoinPlayerGame(int gameId , int playerID)
        //{
        //    var entities = new GameDbEntities();

        //    entities.AddToPlayerGame(new PlayerGameEntity { GameID = gameId, PlayerID = playerID });

        //    var result = entities.SaveChanges();

        //    if (result == null)
        //    {
        //        return false;
        //    }

        //    return true;
        //}
    }
}
